/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ThemDonGiaFrm.java
 *
 * Created on May 29, 2011, 4:42:19 AM
 */
package gui;

import dao.DonGiaDAO;
import dao.HangVeDAO;
import dao.TuyenBayDAO;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import pojo.DonGiaPOJO;
import pojo.HangVePOJO;
import pojo.TuyenBayPOJO;

/**
 *
 * @author b2
 */
public class QDDonGiaHangVeFrm extends javax.swing.JFrame {

    /** Creates new form ThemDonGiaFrm */
    public QDDonGiaHangVeFrm() {
        initComponents();
    }
    
    public void initThemDonGia() {
        ArrayList<TuyenBayPOJO> dsTuyenBay = TuyenBayDAO.LayDanhSachTuyenBay();
        String[] cbTuyenBay = new String[dsTuyenBay.size()];
        for (int i = 0; i < dsTuyenBay.size(); i++) {
            cbTuyenBay[i] = dsTuyenBay.get(i).getMaTuyenBay();
        }
        DefaultComboBoxModel tuyenBayModel = new DefaultComboBoxModel(cbTuyenBay);
        this.jcbMaTuyenBay.setModel(tuyenBayModel);
        String maTuyenbay = (String) this.jcbMaTuyenBay.getSelectedItem();
        this.thongTinTuyenBay(maTuyenbay);
        //combobox hạng vé
        ArrayList<HangVePOJO> dsHangVe = HangVeDAO.LayDanhSachDangVe();
        String[] cbHangVe = new String[dsHangVe.size()];
        for (int i = 0; i < dsHangVe.size(); i++) {
            cbHangVe[i] = dsHangVe.get(i).getMaHangVe();
        }
        DefaultComboBoxModel hangVeModel = new DefaultComboBoxModel(cbHangVe);
        this.jcbMaHangVe.setModel(hangVeModel);
        this.txtDonGia.setText("");
    }

    public void thongTinTuyenBay(String maTuyenBay) {
        TuyenBayPOJO tuyenBay = TuyenBayDAO.LayTuyenBay(maTuyenBay);
        this.txtSanBayDi.setText(tuyenBay.getSanBayDi());
        this.txtSanBayDen.setText(tuyenBay.getSanBayDen());
    }

    public void themDonGia() {
        String maTuyenBay = (String) this.jcbMaTuyenBay.getSelectedItem();
        String maHangVe = (String) this.jcbMaHangVe.getSelectedItem();
        double donGia = Double.parseDouble(this.txtDonGia.getText());
        String maDonGia = maTuyenBay + "-" + maHangVe;
        DonGiaPOJO dongia = new DonGiaPOJO(maDonGia, maTuyenBay, maHangVe, donGia);
        boolean kq = DonGiaDAO.themDonGia(dongia);
        if (kq == true) {
            JOptionPane.showMessageDialog(this, "Thêm thành công");
        } else {
            JOptionPane.showMessageDialog(this, "Thêm thất bại");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jcbMaTuyenBay = new javax.swing.JComboBox();
        jcbMaHangVe = new javax.swing.JComboBox();
        txtDonGia = new javax.swing.JTextField();
        btnThemMoi = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtSanBayDi = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtSanBayDen = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm đơn giá");
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setText("THÊM ĐƠN GIÁ");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thêm đơn giá"));

        jLabel2.setText("Mã tuyến bay");

        jLabel3.setText("Mã hạng vé");

        jLabel4.setText("Đơn giá");

        jcbMaTuyenBay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " " }));
        jcbMaTuyenBay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbMaTuyenBayActionPerformed(evt);
            }
        });

        btnThemMoi.setText("Thêm mới");
        btnThemMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemMoiActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        jLabel5.setText("Sân bay đi");

        txtSanBayDi.setEditable(false);

        jLabel6.setText("Sân bay đến");

        txtSanBayDen.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel5)
                            .addComponent(jLabel3))
                        .addGap(55, 55, 55)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtSanBayDi, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                .addComponent(jcbMaTuyenBay, 0, 100, Short.MAX_VALUE))
                            .addComponent(jcbMaHangVe, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(63, 63, 63)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4))
                        .addGap(42, 42, 42)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtSanBayDen)
                            .addComponent(txtDonGia, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 587, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(141, 141, 141)
                        .addComponent(btnThemMoi)
                        .addGap(116, 116, 116)
                        .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jcbMaTuyenBay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtSanBayDi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(txtSanBayDen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jcbMaHangVe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(txtDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThemMoi)
                    .addComponent(btnThoat))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(107, 107, 107)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        this.initThemDonGia();
    }//GEN-LAST:event_formWindowOpened
    
    private void btnThemMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemMoiActionPerformed
        // TODO add your handling code here:
        if (this.txtDonGia.getText() != null) {
            this.themDonGia();
        } else {
            JOptionPane.showMessageDialog(this, "Nhập lại đơn giá");
        }
    }//GEN-LAST:event_btnThemMoiActionPerformed
    
    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed
    
    private void jcbMaTuyenBayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbMaTuyenBayActionPerformed
        // TODO add your handling code here:
        String maTuyenbay = (String) this.jcbMaTuyenBay.getSelectedItem();
        this.thongTinTuyenBay(maTuyenbay);
    }//GEN-LAST:event_jcbMaTuyenBayActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            public void run() {
                new QDDonGiaHangVeFrm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnThemMoi;
    private javax.swing.JButton btnThoat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JComboBox jcbMaHangVe;
    private javax.swing.JComboBox jcbMaTuyenBay;
    private javax.swing.JTextField txtDonGia;
    private javax.swing.JTextField txtSanBayDen;
    private javax.swing.JTextField txtSanBayDi;
    // End of variables declaration//GEN-END:variables
}
